#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_image.h>

int cibleClic(int x,int y);
int verif_intervalle(int x,int a,int b);
void TextAAfficher(int bouton_actuel, char ch[]);

int main(int argc, char *argv[])
{
    int i, j, continuer = 1, bouton_actuel = 0;
    char ch[20];
    SDL_Surface *ecran = NULL, *image[5] = {NULL},*bouton[5] = {NULL},*bouton_clic[5] = {NULL},*bouton_son = NULL, *texte = NULL, *test= NULL;
    SDL_Rect pos_image, pos_bouton[5], pos_son, pos_text, pos_test;
    SDL_Event event;
    TTF_Font *police = NULL;
    SDL_Color TextColor = {0, 0, 0};
    Mix_Music *son[5];

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);
    Mix_VolumeMusic(MIX_MAX_VOLUME );

    ecran = SDL_SetVideoMode(1000, 700, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("Jeu Educatif", NULL);
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

    police = TTF_OpenFont("Resources/fonts/ComicPerso.ttf", 65);
    texte = TTF_RenderText_Blended(police, "Tigre", TextColor);

    son[0] = Mix_LoadMUS("Resources/sounds/Tiger.wav");
    son[1] = Mix_LoadMUS("Resources/sounds/chat.ogg");
    son[2] = Mix_LoadMUS("Resources/sounds/chien.ogg");
    son[3] = Mix_LoadMUS("Resources/sounds/Poule.wav");
    son[4] = Mix_LoadMUS("Resources/sounds/cheval.ogg");

    /* Chargement des images Bitmap dans des surfaces */
    image[0] = IMG_Load("Resources/images/tigre.bmp");
    image[1] = IMG_Load("Resources/images/chat.bmp");
    image[2] = IMG_Load("Resources/images/chien.bmp");
    image[3] = IMG_Load("Resources/images/poule.bmp");
    image[4] = IMG_Load("Resources/images/cheval.bmp");
    bouton[0] = IMG_Load("Resources/bouttons/tigrebt.bmp");
    bouton[1] = IMG_Load("Resources/bouttons/chatbt.bmp");
    bouton[2] = IMG_Load("Resources/bouttons/chienbt.bmp");
    bouton[3] = IMG_Load("Resources/bouttons/poulebt.bmp");
    bouton[4] = IMG_Load("Resources/bouttons/chevalbt.bmp");
    bouton_clic[0] = IMG_Load("Resources/bouttons/tigrebtClic.bmp");
    bouton_clic[1] = IMG_Load("Resources/bouttons/chatbtClic.bmp");
    bouton_clic[2] = IMG_Load("Resources/bouttons/chienbtClic.bmp");
    bouton_clic[3] = IMG_Load("Resources/bouttons/poulebtClic.bmp");
    bouton_clic[4] = IMG_Load("Resources/bouttons/chevalbtClic.bmp");
    bouton_son = IMG_Load("Resources/bouttons/play_sound.bmp");

    pos_image.x = 0;
    pos_image.y = 100;
    SDL_BlitSurface(image[0], NULL, ecran, &pos_image);

    pos_son.x = 740;
    pos_son.y = 20;
    SDL_BlitSurface(bouton_son, NULL, ecran, &pos_son);

    pos_text.x = 300;
    pos_text.y = 20;
    SDL_BlitSurface(texte, NULL, ecran, &pos_text);

    pos_bouton[0].x = 865;
    pos_bouton[0].y = 25;
    SDL_BlitSurface(bouton_clic[0], NULL, ecran, &pos_bouton[0]);
    for(i=1;i<5;i++)
    {
        pos_bouton[i].x = 865;
        pos_bouton[i].y = 25 + (140*i);
        SDL_BlitSurface(bouton[i], NULL, ecran, &pos_bouton[i]);
    }

    SDL_Flip(ecran);
    
    
   


    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
                break;
            case SDL_MOUSEBUTTONUP:
                if (event.button.button == SDL_BUTTON_LEFT)
                {
                    j = cibleClic(event.button.x, event.button.y);
                    if(verif_intrevalle(j,0,4))
                    {    
                        if(Mix_PlayingMusic())
                        {
                            Mix_PauseMusic();
                        }
                        
                        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
                        bouton_actuel = j;

                        TextAAfficher(bouton_actuel, ch);
                        texte = TTF_RenderText_Blended(police, ch, TextColor); 

                        SDL_BlitSurface(texte, NULL, ecran, &pos_text);
                        for(i=0;i<5;i++)
                        {
                            if(i == bouton_actuel)
                            {
                                SDL_BlitSurface(bouton_clic[i], NULL, ecran, &pos_bouton[i]);
                            }
                            else
                            {
                                SDL_BlitSurface(bouton[i], NULL, ecran, &pos_bouton[i]);
                            }
                        }
                        SDL_BlitSurface(bouton_son, NULL, ecran, &pos_son);
                        SDL_BlitSurface(image[bouton_actuel], NULL, ecran, &pos_image);
                        SDL_Flip(ecran);
                    }
                    else if(j == 5)
                    {
                        Mix_PlayMusic(son[bouton_actuel], 1);
                    }
                }
                break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                    case SDLK_ESCAPE: /* Appui sur la touche Echap, on arrête le programme */
                        continuer = 0;
                        break;
                    case SDLK_DOWN:
                        if(bouton_actuel == 4)
                        {
                            bouton_actuel = 0;
                        }
                        else
                        {
                            bouton_actuel++;
                        }

                        if(Mix_PlayingMusic())
                        {
                            Mix_PauseMusic();
                        }

                        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
                        
                        TextAAfficher(bouton_actuel, ch);
                        texte = TTF_RenderText_Blended(police, ch, TextColor); 

                        SDL_BlitSurface(texte, NULL, ecran, &pos_text);
                        for(i=0;i<5;i++)
                        {
                            if(i == bouton_actuel)
                            {
                                SDL_BlitSurface(bouton_clic[i], NULL, ecran, &pos_bouton[i]);
                            }
                            else
                            {
                                SDL_BlitSurface(bouton[i], NULL, ecran, &pos_bouton[i]);
                            }
                        }
                        SDL_BlitSurface(bouton_son, NULL, ecran, &pos_son);
                        SDL_BlitSurface(image[bouton_actuel], NULL, ecran, &pos_image);
                        SDL_Flip(ecran);
                        break;
                    case SDLK_UP:
                        if(bouton_actuel == 0)
                        {
                            bouton_actuel = 4;
                        }
                        else
                        {
                            bouton_actuel--;
                        }

                        if(Mix_PlayingMusic())
                        {
                            Mix_PauseMusic();
                        }

                        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
                        
                        TextAAfficher(bouton_actuel, ch);
                        texte = TTF_RenderText_Blended(police, ch, TextColor); 

                        SDL_BlitSurface(texte, NULL, ecran, &pos_text);
                        for(i=0;i<5;i++)
                        {
                            if(i == bouton_actuel)
                            {
                                SDL_BlitSurface(bouton_clic[i], NULL, ecran, &pos_bouton[i]);
                            }
                            else
                            {
                                SDL_BlitSurface(bouton[i], NULL, ecran, &pos_bouton[i]);
                            }
                        }
                        SDL_BlitSurface(bouton_son, NULL, ecran, &pos_son);
                        SDL_BlitSurface(image[bouton_actuel], NULL, ecran, &pos_image);
                        SDL_Flip(ecran);
                        break;
                }
                break;
        }
    }

    SDL_FreeSurface(ecran);
    SDL_FreeSurface(bouton_son);
    SDL_FreeSurface(texte);
    for(i=0;i<5;i++)
    {
        SDL_FreeSurface(image[i]);
        SDL_FreeSurface(bouton[i]);
        SDL_FreeSurface(bouton_clic[i]);
        Mix_FreeMusic(son[i]);
    } 
    SDL_Quit();
    Mix_CloseAudio();
    TTF_CloseFont(police);
    TTF_Quit();

    return EXIT_SUCCESS;
}

int cibleClic(int x,int y)
{
    if(verif_intrevalle(x,865,935))
    {
        if(verif_intrevalle(y,25,115))
        {
            return 0;
        }
        else if(verif_intrevalle(y,165,255))
        {
            return 1;
        }
        else if(verif_intrevalle(y,305,395))
        {
            return 2;
        }
        else if(verif_intrevalle(y,445,535))
        {
            return 3;
        }
        else if(verif_intrevalle(y,585,675))
        {
            return 4;
        }
        else
        {
            return -1;
        }
    }
    else if ((verif_intrevalle(x,745,795))&&(verif_intrevalle(y,25,75)))
    {
        return 5;
    }
    else
    {
        return -1;
    }
}

int verif_intrevalle(int x,int a,int b)
{
    int exist;

    if ((x>=a)&&(x<=b))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void TextAAfficher(int bouton_actuel, char ch[])
{
    switch(bouton_actuel)
    {
        case 0:
            strcpy(ch,"Tigre");
            break;
        case 1:
            strcpy(ch,"Chat");
            break;
        case 2:
            strcpy(ch,"Chien");
            break;
        case 3:
            strcpy(ch,"Poule");
            break;
        case 4:
            strcpy(ch,"Cheval");
            break;
        default:
            strcpy(ch,"Erreur !");
    } 
}